package cn.cxq.learning.sort;

import java.util.Arrays;

/**
 * 插入排序
 */
public class InsertSort {

    public static int[] printSort(int[] arr){
        int insert; //存放插值
        for (int i = 1; i < arr.length; i++) {
            insert = arr[i];
            for (int j = i - 1; j >= 0; j--) {
                if (insert <= arr[j]) arr[j + 1] = arr[j];
                else {
                    arr[j + 1] = insert;
                    break;
                }
            }
            System.out.println(Arrays.toString(arr));
        }
        return arr;
    }

    public static int[] sort(int[] arr){
        int insert; //存放插值
        for (int i = 1; i < arr.length; i++) {
            insert = arr[i];
            for (int j = i - 1; j >= 0; j--) {
                if (insert <= arr[j]) arr[j + 1] = arr[j];
                else {
                    arr[j + 1] = insert;
                    break;
                }
            }
        }
        return arr;
    }
}
